package com.example.demo.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.model.Coin;
import com.example.demo.service.CoinService;
import com.example.demo.utils.HttpUtil;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import com.sun.jersey.core.util.MultivaluedMapImpl;


@Service
public class CoinServiceImpl implements CoinService{
	
	private static final Logger logger = LoggerFactory.getLogger(CoinServiceImpl.class);
	
	private final static String coinUrl = "https://api.imx.com/api/v1/coin_matching?symbol=&code=azw45a";
	
	private static final String dingdingUrl = "https://oapi.dingtalk.com/robot/send?access_token=4ea8cec17569c41a6e52358250ce37c12d1f9f4c41bcd009a89e11d8188cd842";

	@Override
	public Coin getEthCoin() {
		JSONObject json = HttpUtil.doGet(coinUrl);
		if(null != json) {
			JSONArray jj = json.getJSONArray("data");
			JSONArray jo = jj.getJSONObject(0).getJSONArray("matchcoins");
			List<Coin> coinList = JSONArray.parseArray(jo.toJSONString(), Coin.class);
			for(Coin coin:coinList) {
				if(coin.getPageIdentifier().equals("ETH")) {
					logger.info("----------以太坊价格是"+coin.getPrice()+"----------------");
					if(coin.getPrice().doubleValue()<=100) {
						Map<String, Object> object = new HashMap<>();
				        Map<String, Object> map = new HashMap<>();
				        map.put("content", "以太坊跌至"+coin.getPrice()+"美元一个了，赶快抄老底吧!");
				        object.put("msgtype", "text");
				        object.put("text", map);
				        JSON job = new JSONObject(object);
						HttpUtil.sendMessage(job.toJSONString(), dingdingUrl);
					}
					
				}
			}
			
		}
		
		return null;
	}
	
	public static void main(String[] args) {
		Client client = Client.create();
		client.addFilter(new HTTPBasicAuthFilter("api", "key-89822b4858eb3622fb05d8e3e5475ff6"));
		WebResource webResource = client.resource("http://sms-api.luosimao.com/v1/send.json");
		MultivaluedMapImpl formData = new MultivaluedMapImpl();
		formData.add("mobile", "18336676771");     
		formData.add("message", "请登录官网进行查验【铁壳测试】");
		ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED)
								  .post(ClientResponse.class,formData);
		String textEntity = response.getEntity(String.class);
	    int status = response.getStatus();
	    
		
	}

}
